﻿$(function(){
    var request = moon.getRequest(location.search); //获取url中"?"符后的参数
    var deviceId = request['deviceId'];  //获取设备deviceId
    console.log(deviceId);
    $.getJsonData(contextPath+"/device/"+deviceId,{},{type:"Get"}).done(function (data) {
        if(data.success){
            $(".title").html(data.result.title);
            $("#deviceId_content").html(deviceId);
            //把deviceId参数加入到链接中    获取链接地址，在后面加一个id
            var editNode = $(".edit");//Class为edit的节点  获取节点
            var editHref = editNode.attr("href");//获取a标签的href
            editNode.attr("href",editHref+"?deviceId="+deviceId);

            var addSensorNode = $("#addSensor");//Id=addSensor的节点  获取节点
            var addSensorHref = addSensorNode.attr("href");//获取a标签的href
            addSensorNode.attr("href",addSensorHref+"?deviceId="+deviceId);

            var detailsNode = $(".details");//Id=addSensor的节点  获取节点
            var detailsHref = detailsNode.attr("href");//获取a标签的href
            detailsNode.attr("href",detailsHref+"?deviceId="+deviceId);
        }
    });

    $.getJsonData(contextPath + "/sensor/list/"+deviceId,{},{type:"Get"}).done(function(data){
        if(data.success){
            var result = data.result.items;//取数据列表
            console.log(result)
            var sensorNode = $(".template .sensorContainer");//获得sensor的模板
            $.each(result,function(){
                console.log(this);
                var cloneNode = sensorNode.clone(true);
                cloneNode.find(".sensorNameLeft").html(this.title);
                cloneNode.find(".data-id").html(this.id);

                var typeValue = "数据型";
                if(this.type == 1){
                    typeValue = "GPS";
                }else if(this.type == 2){
                    typeValue = "开关";
                    cloneNode.find(".on-off").removeClass("hide");
                }
                cloneNode.find(".sensorNameRight").html(typeValue);
                cloneNode.attr("data-id",this.id);

                var editNode = cloneNode.find(".linkOne");//Class为edit的节点  获取节点
                var editHref = editNode.attr("href");//获取a标签的href
                editNode.attr("href",editHref+"?sensorId="+this.id);

                //获取传感器最新数据
                var sensorId=this.id;
                var unit=this.unit;
                $(function(){
                    $.getJsonData(contextPath+"/datapoint/"+sensorId,{},{type:"GET"}).done(function(data){
                        if(data.success){
                            var datapointresult=data.result;
                            cloneNode.find("#temperature").html(datapointresult.datapointValue);
                            cloneNode.find("#belowTemperature").html(unit);
                        }
                    })
                })

                $("#sensorList").append(cloneNode);
            })
        }
    }).fail(function(e){
        console.log(e);
    });

    $("#sensorList").on("click",".linkTwo", function (e) {
        var sensorNode = $(e.currentTarget).closest(".sensorContainer");
        var sensorId = sensorNode.attr("data-id");
        console.log(sensorId);
        $.getJsonData(contextPath + "/sensor/"+sensorId, {}, {type: "Delete"}).done(function (data) {
            console.log(data);
            if(data.success){
                window.location.href = contextPath+"/sen_suc_del.html?deviceId="+deviceId;
            }else{
                moon.error("删除失败","top");
            }
        })
    });

    $("#loginOut").on("click",function(){
        $.getJsonData(fullServerPath+"/users/logout",{},{type:"Post"}).done(function(data){
            if(data.success){
                moon.info("退出","top");
                window.location.href = contextPath+"login.html";
            }else{
                moon.error("退出失败","top");
            }
        })
    })

    $(document).ready(function(){

        $("#openButton").bind("click",function(){
            var x = $("#openButton").text();
            if(x=="开"){
                $("#openButton").text("关");
            }
            else{
                $("#openButton").text("开");
            }
        });
    });
})



